<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 * 获取Token接口
 * @author Calvin<hongbinfu@163.com>
 * @readme  :返回结果请使用common_response
 * @help    :具体用法请参考https://github.com/chriskacerguis/codeigniter-restserver
 * @other   :
 *          GET ---  $this->get();
 *          POST --- $this->post();
 *          PUT --- $this->put();
 *         DELETE --- $this->delete();
 */
require(APPPATH.'libraries/Android_Controller.php');
class Token extends Android_Controller
{
    //private $secret;
   // private $session_key = "__ACCESS_TOKEN__";
   /* public function __construct($config = 'rest')
    {
        parent::__construct($config = 'rest');
    }*/

    public function __construct() {
        parent::__construct();
        $services = array(
            'token_service'
        );
        $this->load->service($services);
    }
    /**
     *
     */
    public function index_get()
    {
        $id = $this->get("client_id");
        $secret = $this->get("client_secret");
        if(empty($id) || empty($secret)){
            $this->common_response(1003);
        }
        if($this->token_service->check_secret($id,$secret))
        {
            $sess_access_token = $this->token_service->get_access_token(FALSE);
            $this->common_response(0,NULL,array("access_token"=>rawurlencode($sess_access_token),"expires_in"=>$this->config->item("rest_access_token_expire")));
        }
        else
        {
            $this->common_response(1015);
        }
    }

    public function temp_get(){
        $sess_access_token = $this->token_service->get_access_token(TRUE);
        $this->common_response(0,NULL,array("access_token"=>rawurlencode($sess_access_token),"expires_in"=>$this->config->item("rest_temp_access_token_expire")));
    }



    /**
     * 刷新token
     */
    public function keep_alive_get()
    {
        $key = $this->config->item("rest_access_token_session_key");
        $data = $this->session->userdata($key);
        if (empty($data))
            return $this->common_response(1015);

        $data['time'] = time();
        $this->session->set_userdata($key, $data);
        return $this->common_response(0,NULL, array('data' => $data));
    }
}